/*************************************************
* Title：TinyToolKit
* Author：Opportunity 
* Describe：Transform类的扩展
* CreateTime：2020-04-05 19:39:40
* Version：1.0
* Modify Recorder：
*************************************************/

using UnityEngine;

namespace TinyToolKit
{
    //Unity Transform组件的功能扩展
    public static class TransformExt 
    {
        /// <summary>
        /// 设置绝对位置的X坐标
        /// </summary>
        public static void SetPosX(this Transform transform, float x)
        {
            Vector3 pos = transform.position;
            pos.x = x;
            transform.position = pos;
        }


        /// <summary>
        /// 设置绝对位置的Y坐标
        /// </summary>
        public static void SetPosY(this Transform transform, float y)
        {
            Vector3 pos = transform.position;
            pos.y = y;
            transform.position = pos;
        }

        /// <summary>
        /// 设置绝对位置的Z坐标
        /// </summary>
        public static void SetPosZ(this Transform transform, float z)
        {
            Vector3 pos = transform.position;
            pos.z = z;
            transform.position = pos;
        }

        /// <summary>
        /// 设置绝对位置的XY坐标
        /// </summary>
        public static void SetPosXY(this Transform transform, float x,float y)
        {
            Vector3 pos = transform.position;
            pos.x = x;
            pos.y = y;
            transform.position = pos;
        }

        /// <summary>
        /// 设置绝对位置的XZ坐标
        /// </summary>
        public static void SetPosXZ(this Transform transform, float x,float z)
        {
            Vector3 pos = transform.position;
            pos.x = x;
            pos.z = z;
            transform.position = pos;
        }

        /// <summary>
        /// 设置绝对位置的YZ坐标
        /// </summary>
        public static void SetPosYZ(this Transform transform, float y,float z)
        {
            Vector3 pos = transform.position;
            pos.y = y;
            pos.z = z;
            transform.position = pos;
        }

        /// <summary>
        /// 设置局部坐标的X值
        /// </summary>
        public static void SetLocalPosX(this Transform transform, float x)
        {
            Vector3 localPos = transform.localPosition;
            localPos.x = x;
            transform.localPosition = localPos;
        }

        /// <summary>
        /// 设置局部坐标的Y值
        /// </summary>
        public static void SetLocalPosY(this Transform transform, float y)
        {
            Vector3 localPos = transform.localPosition;
            localPos.y = y;
            transform.localPosition = localPos;
        }

        /// <summary>
        /// 设置局部坐标的Z值
        /// </summary>
        public static void SetLocalPosZ(this Transform transform, float z)
        {
            Vector3 localPos = transform.localPosition;
            localPos.z = z;
            transform.localPosition = localPos;
        }

        /// <summary>
        /// 设置局部坐标的XY值
        /// </summary>
        public static void SetLocalPosXY(this Transform transform, float x, float y)
        {
            Vector3 localPos = transform.localPosition;
            localPos.x = x;
            localPos.y = y;
            transform.localPosition = localPos;
        }

        /// <summary>
        /// 设置局部坐标的XZ值
        /// </summary>
        public static void SetLocalPosXZ(this Transform transform, float x, float z)
        {
            Vector3 localPos = transform.localPosition;
            localPos.x = x;
            localPos.z = z;
            transform.localPosition = localPos;
        }

        /// <summary>
        /// 设置局部坐标的YZ值
        /// </summary>
        public static void SetLocalPosYZ(this Transform transform, float y, float z)
        {
            Vector3 localPos = transform.localPosition;
            localPos.y = y;
            localPos.z = z;
            transform.localPosition = localPos;
        }

        /// <summary>
        /// 重设局部坐标
        /// </summary>
        public static void Reset(this Transform transform)
        {
            transform.localPosition = Vector3.zero;
            transform.localScale = Vector3.one;
            transform.localRotation = Quaternion.identity;
        }

        /// <summary>
        /// 递归遍历所有子对象，查找目标
        /// </summary>
        public static Transform FindTargetInChildren(this Transform transform,string targetName)
        {
            Transform target = null;
            target = transform.Find(targetName);
            if (target == null)
            {
                foreach (Transform child in target.transform)
                {
                    target = child.FindTargetInChildren(targetName);
                    if (target != null) break;
                }
            }
            return target;
        }

        /// <summary>
        /// 设置子节点，并重设子节点局部坐标
        /// </summary>
        public static void SetStandardChild(this Transform transform,Transform child) 
        {
            child.SetParent(transform, false);
            child.Reset();
        }

        /// <summary>
        /// 查找子类对象，如果找不到则创建
        /// </summary>
        /// <param name="transform">需要查找的父类</param>
        /// <param name="childName">需要查找的子类名字</param>
        /// <param name="isIncludeChildren">是否需要查找所有子类</param> 
        public static Transform FindOrCreate(this Transform transform, string childName,bool isIncludeAllChildren = false)
        {
            Transform target = null;
            if (isIncludeAllChildren)
                target = transform.FindTargetInChildren(childName);
            else
                target = transform.Find(childName);

            if(target == null)
            {
                target = new GameObject(childName).transform;
                transform.SetStandardChild(target);
            }
            return target;
        }
        
        /// <summary>
        /// 获取组件，没有组件则增加
        /// </summary>
        /// <returns>返回该组件</returns>
        /// <param name="obj">需要获取组件的对象</param>
        /// <typeparam name="T">组件的类型</typeparam>
        public static T GetOrAddComponent<T>(this Transform obj) where T : Component
        {
            T comp = obj.GetComponent<T>();
            if (comp == null)
                comp = obj.gameObject.AddComponent<T>();

            return comp;  
        }
    }
}
























